package ve.usb.coord.colision;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.coord.colision.hibernate.*;


/**
 * Manejo de elementos de la entidad PlanDeEstudios.<br>
 */
public class AccionesPlanDeEstudios extends CohesionAction {
    
/**
     * Acción llamada por Struts para ejecutar la acción PlanDeEstudiosCoordinacionCrearI.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PlanDeEstudiosCoordinacionCrearI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "planDeEstudiosCoordinacionCrear";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            FPlanDeEstudios fFPlanDeEstudios = (FPlanDeEstudios)sess.getAttribute("FPlanDeEstudios");
            if (fFPlanDeEstudios==null) {
                fFPlanDeEstudios = new FPlanDeEstudios();
                sess.setAttribute("FPlanDeEstudios", fFPlanDeEstudios);
            } else {
                fFPlanDeEstudios.reset(mapping, request);
            }
             
            //Feed for combo listCoordinacion.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(nombre, idCoordinacion) " + 
               "from Coordinacion").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPlanDeEstudios.setListCoordinacion(list);
            }

            fFPlanDeEstudios.setCoordinacion(Long.parseLong((String)request.getParameter("idCoordinacion")));

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PlanDeEstudiosI.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PlanDeEstudiosI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "planDeEstudios";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idPlan");
            Long idPlan = (id==null) ? 
                (Long)request.getSession().getAttribute("idPlan") :
                new Long(id);
            
            HttpSession sess = request.getSession();
            FPlanDeEstudios fFPlanDeEstudios = (FPlanDeEstudios)sess.getAttribute("FPlanDeEstudios");
            if (fFPlanDeEstudios==null) {
                fFPlanDeEstudios = new FPlanDeEstudios();
                sess.setAttribute("FPlanDeEstudios", fFPlanDeEstudios);
            } else {
                fFPlanDeEstudios.reset(mapping, request);
            }
            PlanDeEstudios planDeEstudios = (PlanDeEstudios)s.load(PlanDeEstudios.class, idPlan);
            fFPlanDeEstudios.setIdPlan(planDeEstudios.getIdPlan());

            fFPlanDeEstudios.setCoordinacion(planDeEstudios.getCoordinacion().getIdCoordinacion());

            fFPlanDeEstudios.setCarrera(planDeEstudios.getCarrera());
            fFPlanDeEstudios.setNivel(planDeEstudios.getNivel());

            //Feed for combo listCoordinacion.
            /*{java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(nombre, idCoordinacion) " + 
               "from Coordinacion").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPlanDeEstudios.setListCoordinacion(list);
            }
             */

            Hibernate.initialize(planDeEstudios.getGrupos());
            request.setAttribute("L_Grupo", planDeEstudios.getGrupos());
            Hibernate.initialize(planDeEstudios.getModalidades());
            request.setAttribute("L_Modalidad", planDeEstudios.getModalidades());


            request.getSession().setAttribute("idCoordinacion", fFPlanDeEstudios.getCoordinacion());

            request.getSession().setAttribute("idPlan", idPlan);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PlanDeEstudiosAct.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PlanDeEstudiosAct(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "planDeEstudios";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPlanDeEstudios fFPlanDeEstudios = (FPlanDeEstudios)form;

            PlanDeEstudios planDeEstudios = (PlanDeEstudios)s.load(PlanDeEstudios.class, new Long(fFPlanDeEstudios.getIdPlan()));
            planDeEstudios.setCarrera(fFPlanDeEstudios.getCarrera());
            planDeEstudios.setNivel(fFPlanDeEstudios.getNivel());


            //Feed for combo listCoordinacion.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(nombre, idCoordinacion) " + 
               "from Coordinacion").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPlanDeEstudios.setListCoordinacion(list);
            }

            Hibernate.initialize(planDeEstudios.getGrupos());
            request.setAttribute("L_Grupo", planDeEstudios.getGrupos());
            Hibernate.initialize(planDeEstudios.getModalidades());
            request.setAttribute("L_Modalidad", planDeEstudios.getModalidades());


            request.getSession().setAttribute("idCoordinacion", fFPlanDeEstudios.getCoordinacion());

            request.getSession().setAttribute("idPlan", planDeEstudios.getIdPlan());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PlanDeEstudiosElim.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PlanDeEstudiosElim(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "CoordinacionI";
        final String SALIDA_1 = "planDeEstudios";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idPlan");
            Long idPlan = (id==null) ? 
                (Long)request.getSession().getAttribute("idPlan") :
                new Long(id);
            PlanDeEstudios planDeEstudios = (PlanDeEstudios)s.load(PlanDeEstudios.class, idPlan);

            long idCoordinacion = planDeEstudios.getCoordinacion().getIdCoordinacion();
            planDeEstudios.getCoordinacion().removeFromPlanesDeEstudio(planDeEstudios);

            try {
                s.flush();
                request.getSession().setAttribute("idPlan", idPlan);

                request.getSession().setAttribute("idCoordinacion", idCoordinacion);

            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
                request.setAttribute("msg", "No se pudo eliminar entidad.");
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción PlanDeEstudiosCrear.
     * Registro de la creación de una planDeEstudios.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward PlanDeEstudiosCrear(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_1 = "GrupoPlanDeEstudiosCrearI";

        String salida = SALIDA_1;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPlanDeEstudios fFPlanDeEstudios = (FPlanDeEstudios)form;

            PlanDeEstudios planDeEstudios = new PlanDeEstudios();
            planDeEstudios.setCarrera(fFPlanDeEstudios.getCarrera());
            planDeEstudios.setNivel(fFPlanDeEstudios.getNivel());


            {Coordinacion obj = (Coordinacion)s.load(Coordinacion.class, new Long(fFPlanDeEstudios.getCoordinacion()));
             obj.addToPlanesDeEstudio(planDeEstudios);
            }

            s.flush();
            fFPlanDeEstudios.setIdPlan(planDeEstudios.getIdPlan());
            //Feed for combo listCoordinacion.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(nombre, idCoordinacion) " + 
               "from Coordinacion").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPlanDeEstudios.setListCoordinacion(list);
            }


            //fFPlanDeEstudios.setCoordinacion(planDeEstudios.getCoordinacion().getIdCoordinacion());
            request.getSession().setAttribute("idCoordinacion", fFPlanDeEstudios.getCoordinacion());

            request.getSession().setAttribute("idPlan", planDeEstudios.getIdPlan());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }



}